package dj.ms.support.feign;

import cn.hutool.core.util.ReflectUtil;
import feign.QueryMapEncoder;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * <br>
 * <p>
 * - bean query
 * </p>
 *
 * <br>
 *
 * @author ZJG
 * @version 1.0
 * @date 2021/12/6 上午11:24
 */
public class InheritedQueryMapEncoder implements QueryMapEncoder {

    @Override
    public Map<String, Object> encode(Object object) {
        Class<?> cls = object.getClass();
        Field[] fields = ReflectUtil.getFields(cls);
        Map<String, Object> fieldNameToValue = new HashMap<>(fields.length);
        for (Field field : fields) {
            Object value = ReflectUtil.getFieldValue(object, field);
            if (value != null && value != object) {
                fieldNameToValue.put(field.getName(), value);
            }
        }
        return fieldNameToValue;
    }
}
